#include <avr/io.h>
#include <avr/interrupt.h>
#include <stdlib.h>
#include <avr/wdt.h>
#ifndef F_CPU
#define F_CPU 8000000UL
#endif
#include <util/delay.h>
#include <math.h>
char output2[10] = {0b01111110,0b01001000, 0b00111101, 0b01101101, 0b01001011, 0b01100111, 0b01110111, 0b01001100, 0b01111111, 0b01101111};
float temp = 0;
float number = 0;
uint8_t digit1 = 0;
uint8_t digit2 = 0;
uint8_t digit3 = 0;
uint16_t ctr = 0;
uint16_t threshold =500;

void calc(void)
{
    temp = number;
    temp *= 10;
    digit3 = temp % 10; //last digit
    temp /= 10;
    digit2 = temp % 10;//second digit
    temp /= 10;
    digit1 = temp;//first digit
}
void AdcInit(void) //free running
{
    ADCSRA |= (1<<ADPS2) | (1<<ADPS1);
    ADMUX |= (1<<REFS0);
   // ADMUX |= (1<<ADLAR);
    ADCSRA |= (1<<ADFR);
    ADCSRA |= (1<<ADEN);
    ADCSRA |= (1<<ADSC);
}
int main (void)
{
    calc();
    DDRD = 0xFF;
    DDRB = 0b00000111;

    //pb0 last digit
    //pb1 second digit
    //pb2 first digit
    AdcInit();
    while (1)
    {
        PORTB = 0b00000001;
        PORTD = output2[digit3];

        calc();
        PORTD = 0;
        PORTB = 0;

        PORTB = 0b00000010;
        PORTD = output2[digit2];

        calc();
        PORTD = 0;
        PORTB = 0;

        PORTB = 0b00000100;
        PORTD = output2[digit1];

        calc();
        PORTD = 0;
        PORTB = 0;
        number = ADCW * 5 / 1024; //Vin = ADC * VREF /1024     ADC = VIN*1024/Vref
//        ctr++;
//        if (ctr == threshold)
//        {
//            ctr = 0;
//            // threshold--;
//            if (number >= 999)
//            {
//                number = 0;
//            }
//            else
//            {
//                number++;
//            }
//        }


    }
}
